Nested code - blocks of code inside blocks of code - is eventually necessary, but increases complexity. This is why keeping the code as flat as
possible, by avoiding unnecessary nesting, is considered a good practice.
Merging if
statements when possible will decrease the nesting of the code and improve its readability.
Code like
if condition1 {
if condition2 {
doSomething()
}
}
if let y = someOptional {
if x > 0 {
doSomething()
}
}
Will be more readable as
if condition1 && condition2 {
doSomething()
}
if let y = someOptional where x > 0 {
doSomething()
}
if x > 0, let y = someOptional {
doSomething()
}